<template>
	<page-body>
		<view class="page">
			<view class="flex benben-position-layout flex flex-wrap align-center talk_flex_0"
				:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
				<view class='flex flex-wrap align-center justify-between flex-sub talk_fd0_0'>
					<view class='flex flex-wrap align-center talk_fd0_0_c0'>
						<text class='fu-iconfont2  talk_fd0_0_c0_c0' @tap.stop="handleJumpDiy" data-type="back"
							data-url="1">&#xE794;</text>
					</view>
					<view class='flex flex-wrap align-stretch justify-center'>
						<text class='talk_fd0_0_c1_c0'>签到 </text>
					</view>
					<view class='flex flex-wrap align-center justify-end talk_fd0_0_c0'>
					</view>
				</view>
			</view>
			<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
			<view class="">
				<view class="title-01">
					签到码签到
				</view>
				<view class="title-02">
					请输入发起者提供的签到码完成签到
				</view>
				<view class="gesture-container">
					<!-- 验证码格子区域 -->
					<view class="code-box">
						<view v-for="(item, index) in codeLength" :key="index" class="code-item"
							:class="{ active: codeValue.length === index }" :style="{ color: textColor }">
							<!-- 显示输入的字符 -->
							{{ codeValue[index] ? codeValue[index] : '' }}

							<!-- 当前光标位置显示竖线（模拟光标闪烁） -->
							<view v-if="isFocus && codeValue.length === index" class="cursor"></view>
						</view>

						<!-- 透明输入框（放在格子区域上，负责唤起键盘） -->
						<input class="real-input" type="number" :maxlength="codeLength" v-model="codeValue"
							@input="onInput" @focus="onFocus" @blur="onBlur" />

					</view>

				</view>
			</view>
			<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
			<!---flex布局flex布局开始-->
			<view class="flex flex-direction flex-wrap align-stretch benben-flex-layout talk_flex_1">

			</view>
			<!---flex布局flex布局结束-->
		</view>
	</page-body>
</template>
<script>
	export default {
		data() {
			return {
				codeValue: "", // 输入的验证码
				codeLength: 6, // 验证码长度
				isFocus: true, // 是否聚焦
				textColor: "#000", // 默认文字颜色
				password: "123456", // 正确的验证码
				teaching_id: "",
				check_password: "",
				msgid:'',//系统ing消息过来的签到
			};
		},
		onLoad(options) {
			this.msgid = options.msgid
			this.teaching_id = options.teaching_id
			this.codeLength = options.check_password.split(",").length
			console.log(this.codeLength)
		},
		methods: {
			async onInput(e) {
				this.codeValue = e.detail.value;

				// 输入满位时校验
				if (this.codeValue.length === this.codeLength) {
					const result = this.codeValue

					let datastate = await this.$api.post(global.apiUrls.post68aecbf3b2237, {
						teaching_id: this.teaching_id,
						check_password: String(result).split("").join(",")
					});
					if(this.msgid){
						this.getred()
					}
					// 成功1 
					if (datastate.data.code == 1) {
						this.textColor = "#0EC655"; // 正确绿色
						uni.showToast({
							title: "签到成功",
							icon: "none"
						});

						setTimeout(() => {
							this.$urouter.redirectTo(
								`/pages/course/resultSign/resultSign?code=${datastate.data.code}&time=${datastate.data.time}`
							);
						}, 1000);
					}
					// 超时2
					else if (datastate.data.code == 2) {
						this.textColor = "#CB3734";
						uni.showToast({
							title: "签到失败，已超时",
							icon: "none"
						});

						setTimeout(() => {
							this.$urouter.redirectTo(
								`/pages/course/resultSign/resultSign?code=${datastate.data.code}&time=${datastate.data.time}`
							);
						}, 1000);
					}
					// 失败
					else if (datastate.data.code == -1) {
						this.textColor = "#CB3734"; // 错误红色
						uni.showToast({
							title: "数字错误，请重试",
							icon: "none"
						});
						// 1秒后清空重新输入
						setTimeout(() => {
							this.codeValue = "";
							this.textColor = "#000"; // 恢复默认颜色
							this.isFocus = true;
						}, 1000);
					}
				}
			},
			// 公告 已读
			async getred(){
				let datalist = await this.$api.get(global.apiUrls.post68413be3159e9, {
					aid: this.msgid,
				});
							
				// if (datalist.data.code != 1) {
				// 	this.$message.info(datalist.data.msg);
				// 	return
				// }
			},
			onFocus() {
				this.isFocus = true;
			},
			onBlur() {
				this.isFocus = false;
			}
		}
	};
</script>


<style lang="scss" scoped>
	.container {
		padding: 60rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.title {
		font-size: 32rpx;
		margin-bottom: 40rpx;
	}

	.code-box {
		position: relative;
		display: flex;
		justify-content: center;
	}

	.code-item {
		width: 72rpx;
		height: 98rpx;
		border: 2rpx solid #ccc;
		margin: 0 10rpx;
		font-size: 52rpx;
		text-align: center;
		font-weight: 500;
		line-height: 98rpx;
		border-radius: 8rpx;
		position: relative;
	}

	.code-item.active {
		border-color: #145EFD;
	}

	/* 光标效果 */
	.cursor {
		position: absolute;
		left: 50%;
		top: 32rpx;
		width: 2rpx;
		height: 34rpx;
		background-color: #145EFD;
		animation: blink 1s infinite;
		transform: translateX(-50%);
	}

	@keyframes blink {

		0%,
		50%,
		100% {
			opacity: 1;
		}

		25%,
		75% {
			opacity: 0;
		}
	}

	/* 真实输入框：透明但可点击 */
	.real-input {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}

	.gesture-container {
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		margin-top: 192rpx;

		padding: 60rpx;

	}

	.title-01 {
		font-weight: 500;
		font-size: 36rpx;
		color: #333333;
		margin-top: 106rpx;
		text-align: center
	}

	.title-02 {
		font-weight: 400;
		font-size: 28rpx;
		color: #999999;
		margin-top: 20rpx;
		text-align: center
	}

	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #FFF;
		background-size: 100% auto;
	}

	.talk_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.talk_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 700;
		color: #333333;
		line-height: 50rpx;
	}

	.talk_fd0_0_c0_c0 {
		font-size: 36rpx;
		color: #333;
	}

	.talk_fd0_0_c0 {
		width: 120rpx;
	}

	.talk_fd0_0 {
		padding: 0rpx 32rpx 0rpx 32rpx;
	}

	.talk_flex_1 {
		padding: 24rpx;
	}
</style>